package commander;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;


import core.*;
import gitobject.Commit;
import repository.Repository;



public class CLI {	
	
	/**
	 * Command 'jit init'
	 * @param args
	 * @throws IOException
	 */
	public static void jitInit(String[] args) throws IOException {
		String path = "";
		if(args.length <= 2) { //get default working path
			path = new File(".").getCanonicalPath();
			JitInit.init(path);
		}else if(args[2].equals("-help")){ //see help
			System.out.println("usage: jit init [<path>] [-help]\r\n" +
					"\r\n" +
					"jit init [<path>]:	Create an empty jit repository or reinitialize an existing one in the path or your default working directory.");
		}else {
			path = args[2];
			if(!new File(path).isDirectory()) { //if the working path input is illegal
				System.out.println(path + "is not a legal directory. Please init your reposiroty again. See 'jit init -help'.");
			}else {
				JitInit.init(path);
			}
		}
	}

	public static void jitAdd(String[] args) {
		String workDir = Repository.getWorkTree();

		if(args.length <= 2 || (args.length > 2 && args[2].equals("-help"))) {
			System.out.println("usage: jit add <file1> [<file2>...] [-help]\r\n" +
					"\r\n" +
					"jit add <file1> [<file2>...]: Add file(s) to stage.");
		}else {
			for(int i = 2; i < args.length; i++) {
				String fileName = args[i];
				File file = new File(workDir + File.separator + fileName);
				try {
					JitAdd.add(file);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void jitRm(String[] args) {
		String workDir = Repository.getWorkTree();

		if(args.length <= 2 || (args.length > 2 && args[2].equals("-help"))) {
			System.out.println("usage: jit rm <file1> [<file2>...] [-help]\r\n" +
					"\r\n" +
					"jit rm <file1> [<file2>...]: Remove file(s) from stage.");
		}else {
			for(int i = 2; i < args.length; i++) {
				String fileName = args[i];
				File file = new File(workDir + File.separator + fileName);
				try {
					JitRm.remove(file);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Command 'jit commit-m'
	 * @param args
	 * @throws Exception
	 */
	public static void jitCommit(String[] args) throws Exception {

		String message = "";
		if(args.length <= 2 || args[2].equals("-help")) { //see help
			System.out.println("usage: jit commit-m [message] [-help]\r\n" +
					"\r\n" +
					"jit commit-m [message]: Commit files to staged area (.jit/object).");
		}else {
			message = args[2];
			JitCommit.commit(message);
		}
	}

	/**
	 * Command 'jit log'
	 * @param args
	 * @throws Exception
	 */
	public static void jitLog(String[] args) throws Exception {

		if(args.length > 2 && args[2].equals("-help")) { //see help
			System.out.println("usage: jit log [-help]\r\n" +
					"\r\n" +
					"jit log: Record every commit belongs to current branch.");
		}else JitLog.log();
	}

	public static void jitBranch(String[] args) throws Exception {

		String branchName = "";
		if (args.length <= 2) { // get default working path
			System.out.println(" ");
		}  else if (args[2].equals("-help")) { // see help
			System.out.println("usage: jit branch [<branchName>] [-help]\r\n" + "\r\n"
					+ "jit branch [<branchName>]:	Create a branch with name:<branchName>");
		}  else if(args[2].equals("-d")){
			JitBranch.deleteBranch(args[3]);
		}  else {
			branchName = args[2];
			if (Commit.getLastCommit() != null)
				JitBranch.branch(branchName);
			else
				System.out.print("Not ever committed even once!");
		}
	}


	/**
	 * Command 'jit reset'
	 *
	 * @param args
	 * @throws Exception
	 */
	public static void jitReset(String[] args) throws Exception {
		String path = "";
		if (args.length <= 2) {
			System.out.println("Invalid Command!");
		} else if (args[2].equals("-help")) { // see help
			System.out.println("usage: jit init [<path>] [-help]\r\n" + "\r\n"
					+ "jit init [<path>]:	Create an empty jit repository or reinitialize an existing one in the path or your default working directory.");
		} else if (args[2].equals("--hard")) { // jit reset --hard
			// 可能会报数组越界错误
			path = args[3];
			JitReset.hard(path);
		} else if (args[2].equals("--soft")) { // jit reset --hard
			// 可能会报数组越界错误
			path = args[3];
			JitReset.soft(path);
		} else if (args[2].equals("--mixed")) { // jit reset --hard
			// 可能会报数组越界错误
			path = args[3];
			JitReset.mixed(path);
		} else {
			path = args[2];
			File file = new File(path);
			JitAdd.add(file);
		}
	}

	public static void jitCheckout(String[] args) throws Exception {

		String branchName = "";
		if (args.length <= 2) { // get default working path
			System.out.println(" ");
		} else if (args[2].equals("-help")) { // see help
			System.out.println("usage: jit branch [<branchName>] [-help]\r\n" + "\r\n"
					+ "jit branch [<branchName>]:	Create a branch with name:<branchName>");
		} else {
			branchName = args[2];
			JitCheckout.checkout(branchName);
		}
	}

	public static void jitStatus(String[] args) throws Exception {
		String path = "";
		if (args.length <= 2) { // get default working path
			System.out.println(" ");
		} else if (args[2].equals("-help")) { // see help
			System.out.println("usage: jit status [<path>] [-help]\r\n" + "\r\n"
					+ "jit status [<path>]:	Create an empty jit repository or reinitialize an existing one in the path or your default working directory.");
		} else {
			path = args[2];
			JitStatus.status(path);
		}
	}

	/**
	 * Command 'jit help'.
	 */
	public static void jitHelp() {
		System.out.println("usage: jit [--version] [--help] [-C <path>] [-c name=value]\r\n" +
				"           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\r\n" +
				"           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\r\n" +
				"           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\r\n" +
				"           <command> [<args>]\r\n" +
				"\r\n" +
				"These are common Jit commands used in various situations:\r\n" +
				"\r\n" +
				"start a working area\r\n" +
				"   init       Create an empty Jit repository or reinitialize an existing one\r\n" +
				"\r\n" +
				"work on the current change\r\n" +
				"   add        Add file contents to the index\r\n" +
				"   reset      Reset current HEAD to the specified state\r\n" +
				"   rm         Remove files from the working tree and from the index\r\n" +
				"\r\n" +
				"examine the history and state\r\n" +
				"   log        Show commit logs\r\n" +
				"   status     Show the working tree status\r\n" +
				"\r\n" +
				"grow, mark and tweak your common history\r\n" +
				"   branch     List, create, or delete branches\r\n" +
				"   checkout   Switch branches or restore working tree files\r\n" +
				"   commit     Record changes to the repository\r\n" +
				"   diff       Show changes between commits, commit and working tree, etc\r\n" +
				"   merge      Join two or more development histories together\r\n" +
				"\r\n" +
				"'jit help -a' and 'jit help -g' list available subcommands and some\r\n" +
				"concept guides. See 'jit help <command>' or 'jit help <concept>'\r\n" +
				"to read about a specific subcommand or concept.");
	}
	
	public static void main(String[] args) throws Exception {
		while (true) {
			System.out.println("输入命令:");
			Scanner scanner = new Scanner(System.in);
			String str = scanner.nextLine();
			args = str.split("\\s+");
			System.out.println(args.length);
			if (args.length <= 1 || args[1].equals("help")) {
				jitHelp();
			} else {
				if (args[1].equals("init")) {
					jitInit(args);
				} else if (args[1].equals("add")) {
					jitAdd(args);
				} else if (args[1].equals("rm")) {
					jitRm(args);
				} else if (args[1].equals("commit-m")) {
					jitCommit(args);
				} else if (args[1].equals("log")) {
					jitLog(args);
				} else if (args[1].equals("branch")) {
					jitBranch(args);
				} else if (args[1].equals("checkout")) {
					jitCheckout(args);
				} else if (args[1].equals("reset")) {
					jitReset(args);
				} else if (args[1].equals("status")) {
					jitStatus(args);
				} else {
					System.out.println("jit: " + args[1] + "is not a git command. See 'git help'.");
				}
			}
		}
	}
}
